#!/usr/bin/env python3
# coding:utf-8
import csv
import io
def handle_proc_cpu_by_csv(csv_data):
    csv_file = io.StringIO(csv_data)
    csv_reader = csv.reader(csv_file)
    idx_buf = next(csv_reader)
    name_idx = idx_buf.index('name')
    cpu_idx = idx_buf.index('cpu_percent')
    time_idx = idx_buf.index('time')
    proc_list = {}
    x_axis = []
    first_proc_name = ""
    
    for row in csv_reader:
        # 时间戳更新
        if not first_proc_name:
            first_proc_name=row[name_idx]
            x_axis.append(row[time_idx])
        elif first_proc_name == row[name_idx]:
            x_axis.append(row[time_idx])

        if not row[name_idx] in proc_list:
            proc_list[row[name_idx]] = []
        # 统计各进程cpu占用
        proc_list[row[name_idx]].append(row[cpu_idx])

    csv_file.close()
    proc_list.update({"x_axis": x_axis})
    return proc_list

if __name__ == "__main__":
    with open('process.csv', mode='r') as csvfile:
        csv_data = csvfile.read()
        handle_proc_cpu_by_csv(csv_data)
